<?php
/**
  * tframe
  *
  *
  * @copyright Copyright (c) 2012 - 2014, huxiaomin
  * @author huxiaomin    2012-7-9  
  * @since		Version 1.0
  *
  */
    
class WRouteApp{
	private $jsy_route;
	
	function __construct(){
	   $this->jsy_route = new TTable('jsy_route');
	   $this->routeimageurl = '';
	}
	
	function hook($api){
	   if ($api == 'editRoute'){
	       return array('ajax'=>1);
	   }
	   elseif ($api == 'addImage'){
	       return array('ajax'=>1);
	   }
	   elseif ($api == 'delImage'){
	       return array('ajax'=>1);
	   }
	}

	/**
	 * 线路列表页
	 * @param $request
	 */
    function listRoute($request){
        $list = $this->jsy_route->get('id,name,ico,addtime',array(array('mark','0')));
        return array('list'=>$list); 
    }
    
    /**
     * 页面主要信息
     * @param $request
     */
    function showRoute($request){
        $route_id = intval($request['id']);
        $route = $this->jsy_route->get('*',array(array('id',$route_id)));
        $url = '/adminweb/w-route/editRoute';
        
        $jsy_route_type = new TTable('jsy_route_type');
        $type = $jsy_route_type->get('id,name',array(array('parent_id',0)));
        return array(
                    'route'=>$route[0],
                    'url'=>$url,
                    'type_json'=>json_encode($type),
                    'routeimageurl'=>$this->routeimageurl.'?id='.$route_id,
                    );
    }
    /**
     * 编辑线路
     * @param $request
     */
    function editRoute($request){
    	$form = $request['form'];
    	$route_id = intval($form['id']);
    	if ($route_id){
    	   $return = $this->jsy_route->update($form);
    	}
    	else {
    	   $return = $this->jsy_route->insert($form);
    	}
        return $return;
    }
    
    /**
     * 线路详情页
     * @param $request
     */
    function detail($request){
        return $this->showRoute($request);
    }
    
    /*
     * 线路图片
     */
    function image($request){
        $route_id = $request['id'];
    	$image_dir = TT_DIR."res/upload/image/route/{$route_id}";
    	//$image_dir = 'D:\\Users\\huxiaomin\\Pictures';
    	$image_list = TFile::getImages($image_dir);
        $return = $this->showRoute($request);
        $return['image_list'] = $image_list;
        return $return;
    }
    
    /**
     * 新增图片
     * @param $request
     */
    function addImage($request){
        $file = $_FILES['file'];
        $id = intval($request['id']);
        $TUpload = new TUpload();
        $save = array(
            'file'=>$file,
            'to' => 'res/upload/image/route/'.$id,
        );
        $save_out = $TUpload->save($save);
        return json_encode($save_out);        
    }
    
    /**
     * 删除图片
     * @param $request
     */
    function delImage($request){
        $ico = trim($request['ico']);
        $id = intval($request['id']);
        $img = TT_DIR.substr($ico,1);
        if (!file_exists($img)) {
        	return goutJson(0,'','img not exists '.$ico);
        }
        $unlink = unlink($img);
        return goutJson($unlink);   
    }
    
    
}